package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.BlackListMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 通过用户id分页查询
     *
     * @param loginUserId
     * @return
     */
    @Override
    public PageResult findPageByUserId(Long loginUserId, Long page, Long pageSize) {
        //1. 构建分页参数设置
        Page<BlackList> blackListPage = new Page<>(page,pageSize);
        //2. 构建查询的条件
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",loginUserId);
        //3. 设置按创建时间降序排序
        queryWrapper.orderByDesc("created");
        //4. 查询
        blackListMapper.selectPage(blackListPage, queryWrapper);
        //5. 构建pageResult
        PageResult<BlackList> pageResult = new PageResult<>();
        //5.1 设置page, pageSize, pages总页数, 分页结果集
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setItems(blackListPage.getRecords());
        pageResult.setCounts(blackListPage.getTotal());
        pageResult.setPages(blackListPage.getPages());
        return pageResult;
    }

    /**
     * 移除黑名单
     *
     * @param loginUserId
     * @param blackUserId
     */
    @Override
    public void remove(Long loginUserId, Long blackUserId) {
        //1. 构建删除的条件
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",loginUserId);
        queryWrapper.eq("black_user_id", blackUserId);
        //2. 删除
        blackListMapper.delete(queryWrapper);
    }
}
